; 1 Expressions
486

(+ 100 20.10)

(* 1 2 10)

(+ (* 15 2) (+ 1 2 3))

(+ (* 10
        (/ 3 7))
    (- 10 7))
; 2 Naming and Environment
(define height 100)

height

; 3 Evaluating Combination
(* (+ 10 (- 10 (* 2 3)))
    (/ height 3.2))


; 4 Compound Procedures
(define (square x) (* x x))

square

(square 21)

(define (sum-of-square x y)
    (+ (square x) (square y))
)

(sum-of-square 3 4)

(define (f x)
    (sum-of-square (+ x 1)  (square x))
)

(f 3)

; 6 Conditional Exprs and Predicates
(define (abs x)
    (cond ((> x 0) x)
        ((= x 0) 0)
        ((< x 0) (- x))
    )
)

(abs 0.1)

(define (abs x)
    (cond ((< x 0) (- x))
        (else x))
)
abs

(define (abs x)
    (if (< x 0)
        (- x)
        x)
)
(abs -100)
; we can define >= by ourselves!
(define (>= x y)
    (or (> x y) (= x y)))
>=
(define (>= x y)
    (not (< x y)))

; 7 Example: finding square root
(define (sqrt-iter x guess)
    (if (good-enough? (square guess) x)
        guess
        (sqrt-iter x (average guess (/ x guess))))
)
(define (average x y)
    (/ (+ x y) 2))
(define (good-enough? a b)
    (and (< (- a b) 0.01) (< (- b a) 0.01)) )

(sqrt-iter 100 1.)

; 8 Procedure as Black-Box Abstraction
; Local name
; Internal definition and block structure
(define (square-sum x y)
    (define (square x)
        (* x x))
    (+ (square x) (square y))
)
(square-sum 10 1)